#!/bin/bash
##
# Date: 2018-03-05
# Author: phiix
# Describe: compile and install nginx-1.12.2
##
trap "{ echo Interrupt by keyboard Ctrl-C;exit 1 ; }" SIGINT

export PATH=/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin
export LANG=en_US.UTF-8

current_dir=`pwd`
config_path="${current_dir%/*}/configs/nginx"
install_log="/root/nginx_install.log"

check_env () {
echo "Check and Install Compile tools and dependency packages." 

rpm -qa|grep -Pq "^gcc" 
if [ $? -ne 0 ];then
	yum install -y gcc gcc-g++ 
fi

rpm -qa|grep -Pq "^pcre-devel" 
if [ $? -ne 0 ];then
	yum install -y pcre pcre-devel 
fi

rpm -qa|grep -Pq "^openssl-devel" 
if [ $? -ne 0 ];then
	yum install -y openssl openssl-devel
fi

rpm -qa|grep -Pq "^zlib"
if [ $? -ne 0 ];then
	yum install -y zlib zlib-devel
fi
}

cd /usr/local/src
index=1
for nginx_source in `find * -name "nginx*.tar.*" -print`
do
    nginx[index++]="$nginx_source"
done

if [ "x${nginx[1]}" == "x" ];then
	echo "No available nginx source package."
	echo "Please put the nginx source package to /usr/local/src directory."
	echo "Exit..."
	exit 1
fi

clear
echo "Available nginx source package list: "

for index in ${!nginx[@]}
do
	echo "$index) ${nginx[index]}"
done

read -t 10 -p "Choice one nginx version to install: " choice
if [[ $choice =~ [1-9][0-9]* ]] && [ $choice -le $index ];then
	nginx_version=${nginx[choice]%.tar*}
	echo "${nginx_version} will be installed."
else
	echo "Error choice."
	echo "Exit..."
	exit 1
fi

cd /usr/local
ls |grep -Pq ${nginx_version} && { echo "${nginx_version} has installed.Exit...";exit 1 ; } 


clear
echo -e "--->Start install $nginx_version<---" |tee ${install_log}

check_env 2>&1 |tee -a ${install_log} 

nginx_user=wwwpub
id ${nginx_user} >/dev/null 2>&1
if [ $? -ne 0 ];then
	groupadd -r  ${nginx_user}
	useradd -r -g ${nginx_user} -s /usr/sbin/nologin ${nginx_user}
	mkdir -p /home/${nginx_user} && chown -R ${nginx_user}.${nginx_user} /home/${nginx_user}
fi

cd /usr/local/src

[ -f ${nginx[choice]} ] && tar xvf ${nginx[choice]}
cd ${nginx_version}

compile_args="--prefix=/usr/local/${nginx_version} \
--conf-path=/usr/local/${nginx_version}/etc/nginx.conf \
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--pid-path=/var/run/nginx.pid \
--lock-path=/var/run/nginx.lock \
--http-client-body-temp-path=/var/cache/nginx/client_temp \
--http-proxy-temp-path=/var/cache/nginx/proxy_temp \
--http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp \
--http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp \
--http-scgi-temp-path=/var/cache/nginx/scgi_temp \
--user=${nginx_user} \
--group=${nginx_user} \
--with-compat \
--with-threads \
--with-http_addition_module \
--with-http_auth_request_module \
--with-http_dav_module \
--with-http_flv_module \
--with-http_gunzip_module \
--with-http_gzip_static_module \
--with-http_mp4_module \
--with-http_random_index_module \
--with-http_realip_module \
--with-http_secure_link_module \
--with-http_slice_module \
--with-http_ssl_module \
--with-http_stub_status_module \
--with-http_sub_module \
--with-http_v2_module \
--with-mail \
--with-mail_ssl_module \
--with-stream \
--with-stream_realip_module \
--with-stream_ssl_module \
--with-stream_ssl_preread_module "

cpu_proc=`grep proc /proc/cpuinfo|wc -l`

./configure ${compile_args} && make -j ${cpu_proc} && make install || result=1
if [ ${result:-0} -ne 0 ];then
	echo "Compile error.Exit..."
	exit 1
fi

if [ -d /etc/nginx ];then
	cd /usr/local/${nginx_version}
	mv etc etc.old
	cp -a /etc/nginx ./etc
fi

if [ -L /etc/nginx ];then
	cd /usr/local/${nginx_version}
	mv etc etc.old
	mkdir etc
	cp -a /etc/nginx/* ./etc/
fi

mkdir -p /var/cache/nginx/client_temp
mkdir -p /var/log/nginx

switch_version () {
cd /usr/local

if [ -L nginx ];then
	rm -rf nginx
fi

if [ -d nginx ];then
	mv nginx nginx.`date +%F`
fi

ln -s ${nginx_version} nginx

if [ -d /etc/nginx ] || [ -L /etc/nginx ];then
	rm -rf /etc/nginx
fi

ln -s /usr/local/nginx/etc /etc/nginx
ln -sf /usr/local/nginx/sbin/* /usr/local/sbin/

cd /usr/lib/systemd/system
if [ -f nginx.service ];then
	mv nginx.service nginx.service.`date +%F`
fi
cp ${config_path}/nginx.service .

cd ${current_dir}
nginx -v
nginx -t
if [ $? -ne 0 ];then
	echo "Nginx configure error,Exit..."
	exit 1
fi
echo
echo "Execute: systemctl start nginx ,to start nginx"
echo "Execute: systemctl enable nginx ,to add nginx.service start when system started"
}

read -t 15 -p "Switch nginx version ?[y/n] (default: n)  " reply
if [ "${reply:-n}" == "y" ];then
	switch_version
else
	echo "Do not switch nginx version."
fi

echo -e "\n--->Install Completed, OK!<---"

exit 0
